package cn.edu.gzist.chapter09scheduler.utils;

import cn.edu.gzist.chapter09scheduler.entity.JobBean;
import cn.edu.gzist.chapter09scheduler.job.MyJob;
import org.quartz.*;

public class JobUtil {

    public static void crteateJob(Scheduler scheduler, JobBean jobBean) {
        Class<? extends Job> jobClass = null;
        JobDetail jobDetail = null;
        Trigger trigger = null;
        try {
            jobClass = (Class<? extends Job>) Class.forName(jobBean.getJobClass());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        jobDetail = JobBuilder.newJob(jobClass)
                .storeDurably() //持久化
                .withIdentity(jobBean.getJobName(), jobBean.getJobName() + "_group")  //唯一标识
                .usingJobData("count", 1) //初始化
                .build();


        trigger = TriggerBuilder.newTrigger()
                .forJob(jobDetail) // 关联JobDetail
                .withIdentity(jobBean.getJobName() + "_trigger", jobBean.getJobName() + "group1") // 唯一标识
                .withSchedule(CronScheduleBuilder.cronSchedule(jobBean.getCronExpression()))
                .build();
        try {
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    public static void pauseJob(Scheduler scheduler, String jobName) {
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.pauseJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    public static void resumeJob(Scheduler scheduler, String jobName) {
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.resumeJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    public static void deleteJob(Scheduler scheduler, String jobName) {
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.deleteJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    public static void modifyJob(Scheduler scheduler, JobBean jobBean) {

        TriggerKey triggerKey = TriggerKey.triggerKey(jobBean.getJobName() + "_trigger", jobBean.getJobName() + "group1");

        try {
            CronTrigger oldTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            if (oldTrigger == null) {
                
            }
            else{
                String cronExpression = jobBean.getCronExpression();

                CronTrigger newTrigger = oldTrigger.getTriggerBuilder()
                        .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionDoNothing()).build();
                scheduler.rescheduleJob(triggerKey, newTrigger);
            }
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }

    }



    public static void runJobDone(Scheduler scheduler, String jobName) {
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.triggerJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }
}
